<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发布者" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:approved:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="approvedList">

      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="发布者" align="center" prop="publisher" />
      <el-table-column label="发布时间" align="center" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布内容" align="center" prop="content" />
      <el-table-column label="审核状态(已审核)" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['system:approved:query']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script setup name="Approved">
import { listApproved, getApproved } from "@/api/system/approved"

const { proxy } = getCurrentInstance()

const approvedList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    publisher: null,
    status: null, // 先不设置状态，让后端处理
  }
})

const { queryParams } = toRefs(data)

/** 查询已审核列表 */
function getList() {
  loading.value = true
  listApproved(queryParams.value).then(response => {
    approvedList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/approved/export', {
    ...queryParams.value
  }, `approved_${new Date().getTime()}.xlsx`)
}

/** 详情按钮操作 */
function handleDetail(row) {
  // 跳转到详情页面
  proxy.$router.push(`/system/approved/detail/${row.id}`)
}

/** 获取状态类型 */
function getStatusType(status) {
  switch (status) {
    case 0: return 'warning' // 待审核
    case 1: return 'success' // 已上架
    case 2: return 'success' // 已通过审核
    case 3: return 'danger'  // 审核不通过
    case 5: return 'warning' // 活动已下架
    default: return 'info'
  }
}

/** 获取状态文本 */
function getStatusText(status) {
  switch (status) {
    case 0: return '待审核'
    case 1: return '已上架'
    case 2: return '已通过审核'
    case 3: return '审核不通过'
    case 5: return '活动已下架'
    default: return '未知'
  }
}

getList()
</script>
